﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.ComponentModel.Design;

using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using Wicresoft.Shell.Data;
using Wicresoft.Shell.Logic;

namespace Wicresoft.Shell.Package.AppExp
{
    public partial class NewItemForm : Form
    {
        public string selectedTemplateType = string.Empty;
        private Dictionary<string, string> descriptionMapping = new Dictionary<string, string>();
        /// <summary>
        /// itemType : Application\Function\Template
        /// </summary>
        /// <param name="itemType"></param>
        public NewItemForm(string itemType)
        {
            InitializeComponent();

            descriptionMapping.Add("Application", "Application contains sub application and functions");
            descriptionMapping.Add("Function", "Function contains datatemplate adaptertemplate xsltemplate etc");
            descriptionMapping.Add("DataTemplate", "Contains a datatemplate , default name is Data");
            descriptionMapping.Add("AdapterTemplate", "Contains a adaptertemplate");
            descriptionMapping.Add("XSLTemplate", "Contains a xsltemplate");

            foreach (VersionTable vt in Workspace.GetDatabaseLogic().GetVersions())
            {
                cmbAdapterTmpName.Items.Add(vt.CLIENT_SYS_TYPE);
            }

            switch (itemType)
            {
                case "Application":
                    categoryTV.Nodes.Add("Application");
                    templateLV.Items.Add(new ListViewItem("Application",0));
                    break;
                case "Function":
                    categoryTV.Nodes.Add("Function");
                    templateLV.Items.Add(new ListViewItem("Function", 1));
                    label5.Visible = true;
                    txtFunctionCode.Visible = true;
                    break;
                case "Template":
                    categoryTV.Nodes.Add("Template");
                    templateLV.Items.Add(new ListViewItem("DataTemplate", 2));
                    templateLV.Items.Add(new ListViewItem("AdapterTemplate", 3));
                    //templateLV.Items.Add(new ListViewItem("XSLTemplate", 4));
                    break;
                default:
                    categoryTV.Nodes.Add("Application");
                    templateLV.Items.Add(new ListViewItem("Application", 0));
                    break;
            }
            templateLV.Items[0].Selected = true;
        }

        private void Category_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void Template_SelectedChanged(object sender, EventArgs e)
        {
            if (templateLV.SelectedItems.Count != 0)
            {
                descriptionTB.Text = descriptionMapping[templateLV.SelectedItems[0].Text];
                openBtn.Enabled = true;
                switch (templateLV.SelectedItems[0].Text)
                {
                    case "DataTemplate":
                        cmbAdapterTmpName.Visible = false;
                        nameTB.Visible = true;
                        nameTB.Text = string.Empty;
                        nameTB.Enabled = false;
                        break;
                    case "AdapterTemplate":
                        cmbAdapterTmpName.Visible = true;
                        nameTB.Visible = false;
                        nameTB.Text = string.Empty;
                        break;
                    //case "XSLTemplate":
                    //    cmbAdapterTmpName.Visible = false;
                    //    nameTB.Visible = true;
                    //    nameTB.Text = string.Empty;
                    //    nameTB.Enabled = true;
                    //    break;
                    default:
                        cmbAdapterTmpName.Visible = false;
                        nameTB.Visible = true;
                        nameTB.Text = string.Empty;
                        nameTB.Enabled = true;
                        break;
                }
            }
            else
            {
                descriptionTB.Text = string.Empty;
                openBtn.Enabled = false;
            }
        }

        private void Open_Click(object sender, EventArgs e)
        {
            if (templateLV.SelectedItems.Count != 0)
            {
                //in this case , nameTB.Text cannot be empty
                if (cmbAdapterTmpName.Visible == false && nameTB.Enabled == true)
                {
                    if (nameTB.Text == string.Empty)
                    {
                        MessageBox.Show("The name can not be empty!", "Popup", MessageBoxButtons.OK);
                        return;
                    }
                    else if (txtFunctionCode.Text == string.Empty)
                    {
                        MessageBox.Show("The function code can not be empty!", "Popup", MessageBoxButtons.OK);
                        return;
                    }
                }                  
                selectedTemplateType = templateLV.SelectedItems[0].Text;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
                MessageBox.Show("请选择模板！", "提示", MessageBoxButtons.OK);
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void Template_DoubleClick(object sender, EventArgs e)
        {
            Open_Click(sender, e);
        }
    }
}
